﻿Imports System.Windows.Browser

Partial Public Class Login
    Inherits UserControl

    Private Tries As Integer = 0
    ' todo: some sort of real authentication (yes, anyone can crack open this at the client and get the password right now)
    Private password As String = "Izzy"

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnLogin.Click
        If txtPassword.Text = password Then
            Auth(True)
        Else
            txtInvalid.Visibility = Windows.Visibility.Visible
            txtPassword.Focus()
            Tries += 1
            If Tries > 3 Then
                Auth(False)
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Auth(False)
    End Sub

    Private Sub Auth(ByVal auth As Boolean)
        If Not auth Then
            Dim home As Uri = Application.Current.Host.Source
            HtmlPage.Window.Navigate(New Uri(home.Scheme + Uri.SchemeDelimiter + home.Host))
        Else
            Dim ps As PageSwitcher = TryCast(Me.Parent, PageSwitcher)
            ps.Navigate(New MainPage())
        End If
    End Sub

    Private Sub Login_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        System.Windows.Browser.HtmlPage.Plugin.Focus()
        txtPassword.Focus()
    End Sub

    Private Sub Login_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.LostFocus

    End Sub
End Class
